package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;

import data.FixedPackage;
import data.passingData.FixedPackageData;
import data.passingData.PackagePriceData;

public class FixedPackageLogic extends AbstractBL{
	public FixedPackage GetFixedPackage(long id) {
		Key key = KeyFactory.createKey(EntityName.FixedPackage, id);
		try {
			Entity lang = datastore.get(key);
			FixedPackage c = new FixedPackage();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public FixedPackage GetFixedPackage(Entity entity) {
		FixedPackage item = new FixedPackage();
		item.setEntity(entity);
		return item;
	}
	
	public List<FixedPackage> GetList(List<Entity> entities) {
		List<FixedPackage> list = new ArrayList<FixedPackage>();
		if(entities!=null)
		for (Entity e : entities) {
			FixedPackage a = new FixedPackage();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public FixedPackage AddFixedPackage(long hotelId,String availableForString,boolean status,
			String description,boolean isForever,Date from,Date to,long longOfStay,String packageName,
			String packagepriceListString,Date serverdate)
			throws EntityNotFoundException {
		FixedPackage t = new FixedPackage();
		t.setAvailableForString(availableForString);
		t.setAvaliable(true);
		t.setStatus(status);
		t.setDescription(description);
		t.setForever(isForever);
		if(!isForever){
			t.setFrom(from);
			t.setTo(to);
		}
		t.setHotelId(hotelId);
		t.setLongOfStay(longOfStay);
		t.setPackageName(packageName);
		t.setPackagepriceListString(packagepriceListString);
		t.setDate(serverdate);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public FixedPackage EditFixedPackage(long id,String availableForString,boolean status,
			String description,boolean isForever,Date from,Date to,long longOfStay,String packageName,
			String packagepriceListString,Date serverdate) throws EntityNotFoundException{
		
		FixedPackage t = GetFixedPackage(id);
		t.setAvailableForString(availableForString);
		t.setStatus(status);
		t.setDescription(description);
		t.setForever(isForever);
		if(!isForever){
			t.setFrom(from);
			t.setTo(to);
		}
		t.setLongOfStay(longOfStay);
		t.setPackageName(packageName);
		t.setPackagepriceListString(packagepriceListString);
		t.setDate(serverdate);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public List<FixedPackage> GetAllFixedPackage4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.FixedPackage);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterIsArchived = new FilterPredicate("isArchive",FilterOperator.EQUAL,false);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterIsArchived);
		
		q.setFilter(filter).addSort("date", SortDirection.ASCENDING);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	public List<FixedPackage> GetAllFixedPackage4Admin(long hotelId)
	{
		Query q = new Query(EntityName.FixedPackage);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterIsArchived = new FilterPredicate("isArchive",FilterOperator.EQUAL,false);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterIsArchived);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}	
	
	public List<FixedPackage> GetAllArchivedFixedPackage4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.FixedPackage);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterIsArchived = new FilterPredicate("isArchive",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterIsArchived);
		
		q.setFilter(filter).addSort("date", SortDirection.ASCENDING);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	public List<FixedPackage> GetAllArchivedFixedPackage4Admin(long hotelId)
	{
		Query q = new Query(EntityName.FixedPackage);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterIsArchived = new FilterPredicate("isArchive",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterIsArchived);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}	
	
	
	public FixedPackage ChangeStatus(long id) throws EntityNotFoundException{
		FixedPackage a = GetFixedPackage(id);
		a.setStatus(!a.isStatus());
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public FixedPackage ChangeArchivedLogic(long id) throws EntityNotFoundException{
		FixedPackage a = GetFixedPackage(id);
		a.setArchive(!a.isArchive());
		a.setStatus(!a.isStatus());
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public FixedPackage DeleteFixedPackage(long id) throws EntityNotFoundException{
		FixedPackage t = GetFixedPackage(id);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public FixedPackageData GetFixedPackageDataById(long id){
		FixedPackage obj = GetFixedPackage(id);
		FixedPackageData fpData = new FixedPackageData();
		fpData.setId(obj.getId());
		fpData.setAvailableForString(obj.getAvailableForString());
		fpData.setDescription(obj.getDescription());
		fpData.setForever(obj.isForever());
		fpData.setFrom(obj.getFrom());
		fpData.setTo(obj.getTo());
		fpData.setLongOfStay(obj.getLongOfStay());
		fpData.setPackagename(obj.getPackageName());
		
		List<PackagePriceData> ppdList = new ArrayList<PackagePriceData>();
		for(int i=0;i<obj.getPackagepriceList().size();i++){
			PackagePriceData ppd = new PackagePriceData();
			ppd.setId(obj.getPackagepriceList().get(i).getId());
			ppd.setExtrabed(obj.getPackagepriceList().get(i).getExtrabed());
			ppd.setPriceperNight(obj.getPackagepriceList().get(i).getPriceperNight());
			ppd.setRoomtypeName(obj.getPackagepriceList().get(i).getRoomtype().getShortName());
			ppd.setRoomtypeString(obj.getPackagepriceList().get(i).getRoomTypeString());
			ppd.setUpchargePerson(obj.getPackagepriceList().get(i).getUpchargePerson());
			ppdList.add(ppd);
		}
		
		fpData.setPackageprice(ppdList);
		fpData.setStatus(obj.isStatus());
		return fpData;
	}
	
	public FixedPackage UpdateFixedPackage(FixedPackage fpackage)
	{
		Key key = datastore.put(fpackage.getEntity());
		fpackage.setId(key.getId());
		fpackage.setKey(key);
		return fpackage;
	}
}
